package com.njupt.sso.controller;

import com.njupt.common.pojo.CookieUtils;
import com.njupt.common.pojo.JsonUtils;
import com.njupt.common.pojo.TaotaoResult;
import com.njupt.pojo.TbUser;
import com.njupt.sso.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;

/**
 * @Author: XJL
 * @Description:用户处理
 * @Date: Create in 21:06 2018/6/3
 * @Modified By:
 **/
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${COOKIE_TOKEN_KEY}")
    private String COOKIE_TOKEN_KEY;

    /**
     * 用户数据校验
     * @param param：需要检查的参数
     * @param type：参数检查类型
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/check/{param}/{type}")
    public TaotaoResult checkUserData(@PathVariable String param,@PathVariable Integer type){
        TaotaoResult result = userService.checkData(param,type);
        return result;
    }

    /**
     * 用户注册
     * @param tbUser:用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/register",method = RequestMethod.POST)
    public TaotaoResult registerUser(TbUser tbUser){
        TaotaoResult result = userService.registerUser(tbUser);
        return result;
    }

    /**
     * 用户登陆
     * @param username：用户名
     * @param password：用户密码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public TaotaoResult login(String username, String password, HttpServletResponse response,HttpServletRequest request){
        TaotaoResult result = userService.login(username,password);
        //登陆成功设置cookie
        if (result.getStatus()==200){
            CookieUtils.setCookie(request,response,COOKIE_TOKEN_KEY,result.getData().toString());
        }

        return result;
    }

    /**
     * 根据token获取用户信息;需要指定相應的content-type
     * @param token：session的key
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/token/{token}",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUserByToken(@PathVariable String token,String callback){
        TaotaoResult result = userService.getUserByToken(token);
        if (StringUtils.isNotBlank(callback)){
            return callback+"("+ JsonUtils.objectToJson(result)+");";
        }
        return JsonUtils.objectToJson(result);
    }

    /**
     * 用户登出
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/logout/{token}",method = RequestMethod.GET)
    public TaotaoResult logout(@PathVariable String token){
        TaotaoResult result = userService.logout(token);
        return result;
    }
}
